rm(list = ls())
library(data.table)
library(plyr)
library(tidyr)
library(lfe)
library(stargazer)
library(xtable)
library(sandwich)
library(roll)
library(readxl)
library(readr)
library(zoo)
library(texreg)
library(DescTools)

m <- data.table(read_xlsx("../Data/MasterData.xlsx", skip = 1, guess_max = 1e4))
m[, age := 2021 - year]

m[, memrableperiod_ownret := memrableperiod_ownret * 100]
m[, memrableperiod_pret := memrableperiod_pret * 100]
m[, memrableperiod_aret := memrableperiod_aret * 100]
m[, bias := (memrableperiod_pret - memrableperiod_aret)]

m[, retrecall_1day_rate := retrecall_1day_rate * 100]
m[, retrecall_30day_rate := retrecall_30day_rate * 100]
m[, retrecall_1year_rate := retrecall_1year_rate * 100]
m[, retrecall_5year_rate := retrecall_5year_rate * 100]

m[, expret30day_market_rate := expret30day_market_rate * 100]
m[, expret1year_market_rate := expret1year_market_rate * 100]
m[, expret30day_self_rate := expret30day_self_rate * 100]
m[, expret1year_self_rate := expret1year_self_rate * 100]

m[, memrableperiod_begin := as.yearmon(memrableperiod_begin, "%Ym%m")]
m[, memrableperiod_end := as.yearmon(memrableperiod_end, "%Ym%m")]
m[, memrableperiod_dist := as.yearmon(2022 + 2 / 12) - (memrableperiod_begin + memrableperiod_end) / 2]

sample1 <- m[!is.na(retrecall_30day_rate)]
sample2 <- m[!is.na(retrecall_1year_rate)]

# the table reports the adjusted r2 of each regression table
f1 <- felm(expret30day_market_rate ~ factor(age) | gender + education + total_wealth + total_income, sample1)
f2 <- felm(expret1year_market_rate ~ factor(age) | gender + education + total_wealth + total_income, sample2)
f3 <- felm(expret30day_self_rate ~ factor(age) | gender + education + total_wealth + total_income, sample1)
f4 <- felm(expret1year_self_rate ~ factor(age) | gender + education + total_wealth + total_income, sample2)
stargazer(f1, f2, f3, f4,
          align = TRUE, dep.var.labels.include = TRUE,
          omit = c("Constant"), omit.stat = c("LL", "ser"), ord.intercepts = FALSE, no.space = TRUE,
          single.row = FALSE, column.sep.width = "0pt", digits = 3
)
num1 <- c(f1$N, f2$N, f3$N, f4$N)

f1 <- felm(expret30day_market_rate ~ factor(age) | gender + education + total_wealth + total_income +
             accountcheck_freq + newscheck_freq + discussion_freq + num_wechat, sample1)
f2 <- felm(expret1year_market_rate ~ factor(age) | gender + education + total_wealth + total_income +
             accountcheck_freq + newscheck_freq + discussion_freq + num_wechat, sample2)
f3 <- felm(expret30day_self_rate ~ factor(age) | gender + education + total_wealth + total_income +
             accountcheck_freq + newscheck_freq + discussion_freq + num_wechat, sample1)
f4 <- felm(expret1year_self_rate ~ factor(age) | gender + education + total_wealth + total_income +
             accountcheck_freq + newscheck_freq + discussion_freq + num_wechat, sample2)
stargazer(f1, f2, f3, f4,
          align = TRUE, dep.var.labels.include = TRUE,
          omit = c("Constant"), omit.stat = c("LL", "ser"), ord.intercepts = FALSE, no.space = TRUE,
          single.row = FALSE, column.sep.width = "0pt", digits = 3
)
num2 <- c(f1$N, f2$N, f3$N, f4$N)

f1 <- felm(expret30day_market_rate ~ retrecall_30day_rate, sample1)
f2 <- felm(expret1year_market_rate ~ retrecall_1year_rate, sample2)
f3 <- felm(expret30day_self_rate ~ retrecall_30day_rate, sample1)
f4 <- felm(expret1year_self_rate ~ retrecall_1year_rate, sample2)
stargazer(f1, f2, f3, f4,
          align = TRUE, dep.var.labels.include = TRUE,
          omit = c("Constant"), omit.stat = c("LL", "ser"), ord.intercepts = FALSE, no.space = TRUE,
          single.row = FALSE, column.sep.width = "0pt", digits = 3
)
num3 <- c(f1$N, f2$N, f3$N, f4$N)

